package health.lm.com.shop.activity;

import android.Manifest;
import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.RadarChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.RadarData;
import com.github.mikephil.charting.data.RadarDataSet;
import com.github.mikephil.charting.data.RadarEntry;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.IRadarDataSet;
import com.jakewharton.rxbinding2.view.RxView;
import com.lm.sjy.titlebar.widget.CommonTitleBar;
import com.vondear.rxtool.RxDeviceTool;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import health.lm.com.component_base.base.mvp.activity.XActivity;
import health.lm.com.component_base.helper.ImageLoaderHelper;
import health.lm.com.component_base.widget.CircleImageView.CircleImageView;
import health.lm.com.router.ARouterConstant;
import health.lm.com.shop.R;
import health.lm.com.shop.R2;
import health.lm.com.shop.adapter.HLPeopleHonorAdapter;
import health.lm.com.shop.entity.HLPeopleDetailEntity;
import health.lm.com.shop.presenter.HLPeopleDetailPresenter;

/**
 * Created by Android Studio .
 * Project：hlian health.lm.com.shop.activity
 * Date：2019/8/10 9:58
 * Introduce：人脉详情
 */
@Route(path = ARouterConstant.HLPeopleDetailActivity)
public class HLPeopleDetailActivity extends XActivity<HLPeopleDetailPresenter> {
    @Autowired
    public String id;
    @Autowired
    public String name;
    @BindView(R2.id.titlebar)
    CommonTitleBar mTitlebar;
    @BindView(R2.id.civ_avatar)
    CircleImageView mCivAvatar;
    @BindView(R2.id.tv_nickname)
    TextView mTvNickname;
    @BindView(R2.id.tv_tag_1)
    TextView mTvTag1;
    @BindView(R2.id.tv_tag_2)
    TextView mTvTag2;
    @BindView(R2.id.chart)
    RadarChart mChart;
    @BindView(R2.id.rv_list)
    RecyclerView mRvList;
    @BindView(R2.id.textView15)
    TextView mTextView15;
    @BindView(R2.id.textView14)
    TextView mTextView14;
    @BindView(R2.id.textView16)
    TextView mTextView16;
    @BindView(R2.id.textView17)
    TextView mTextView17;
    @BindView(R2.id.textView18)
    TextView mTextView18;
    @BindView(R2.id.textView19)
    TextView mTextView19;
    @BindView(R2.id.textView20)
    TextView mTextView20;
    @BindView(R2.id.textView21)
    TextView mTextView21;
    @BindView(R2.id.textView22)
    TextView mTextView22;
    @BindView(R2.id.textView23)
    TextView mTextView23;
    @BindView(R2.id.textView24)
    TextView mTextView24;
    @BindView(R2.id.textView25)
    TextView mTextView25;
    @BindView(R2.id.textView26)
    TextView mTextView26;
    @BindView(R2.id.textView27)
    TextView mTextView27;
    @BindView(R2.id.textView28)
    TextView mTextView28;
    @BindView(R2.id.textView29)
    TextView mTextView29;
    @BindView(R2.id.textView30)
    TextView mTextView30;
    @BindView(R2.id.textView31)
    TextView mTextView31;
    @BindView(R2.id.textView32)
    TextView mTextView32;
    @BindView(R2.id.textView33)
    TextView mTextView33;
    @BindView(R2.id.textView34)
    TextView mTextView34;
    @BindView(R2.id.iv_send_message)
    ImageView mIvSendMessage;
    @BindView(R2.id.ll_honor)
    LinearLayout mLlHonor;

    @Override
    public int getLayoutId() {
        return R.layout.hl_activity_people_detail;
    }

    @Override
    public HLPeopleDetailPresenter newP() {
        return new HLPeopleDetailPresenter();
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        mTitlebar.setListener((v, action, extra) -> finish());
        mTitlebar.getCenterTextView().setText(name);
        getP().getData(id);
        //setChart();
    }

    /*设置chart信息*/
    private void setChart(List<HLPeopleDetailEntity.DateArrEntity> pChart) {
        mChart.setBackgroundColor(ContextCompat.getColor(context, R.color.white));
        mChart.getDescription().setEnabled(false);
        mChart.setWebLineWidth(1f);
        mChart.setWebColor(ContextCompat.getColor(context, R.color.color_people));
        mChart.setWebLineWidthInner(1f);
        mChart.setWebColorInner(ContextCompat.getColor(context, R.color.color_people));
        mChart.setWebAlpha(100);
        mChart.setDrawWeb(true);
        List<String> activitys = new ArrayList<>();
        List<Float> muns = new ArrayList<>();
        for (HLPeopleDetailEntity.DateArrEntity entity : pChart) {
            activitys.add(entity.getItem());
            muns.add((float) entity.getA());
        }
        //设置蛛网数据
        setChartData(muns);
        mChart.animateXY(1400, 1400, Easing.EaseInOutQuad);

        XAxis xAxis = mChart.getXAxis();
        //xAxis.setTypeface(tfLight);
        xAxis.setTextSize(9f);
        xAxis.setYOffset(0f);
        xAxis.setXOffset(0f);
        xAxis.setValueFormatter(new ValueFormatter() {
            //private final String[] mActivities = new String[]{"影响力", "号召力", "信用度", "组织力", "领导力", "人脉力"};
            private final String[] mActivities = activitys.toArray(new String[]{});

            @Override
            public String getFormattedValue(float value) {
                return mActivities[(int) value % mActivities.length];
            }
        });
        xAxis.setTextColor(ContextCompat.getColor(context, R.color.color_6666666));

        YAxis yAxis = mChart.getYAxis();
        //yAxis.setTypeface(tfLight);
        yAxis.setLabelCount(activitys.size(), false);
        yAxis.setTextSize(9f);
        yAxis.setAxisMinimum(0f);
        yAxis.setAxisMaximum(100f);
        yAxis.setDrawLabels(false);

        Legend l = mChart.getLegend();
        l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
        l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
        l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
        l.setDrawInside(false);
        //l.setTypeface(tfLight);
        l.setXEntrySpace(7f);
        l.setYEntrySpace(5f);
        l.setTextColor(Color.WHITE);
    }

    /*chart数据设置*/
    private void setChartData(List<Float> pChartData) {
        float mul = 100;
        float min = 0;
        int cnt = pChartData.size();

        ArrayList<RadarEntry> entries1 = new ArrayList<>();
        // NOTE: The order of the entries when being added to the entries array determines their position around the center of
        // the chart.
        for (int i = 0; i < cnt; i++) {
            //float val1 = (float) (Math.random() * mul) + min;
            float val1 = pChartData.get(i);
            entries1.add(new RadarEntry(val1));
        }

        RadarDataSet set1 = new RadarDataSet(entries1, "");
        set1.setColor(ContextCompat.getColor(context, R.color.color_fssf4546));
        set1.setFillColor(ContextCompat.getColor(context, R.color.white));
        set1.setDrawFilled(false);
        set1.setFillAlpha(180);
        set1.setLineWidth(2f);
        set1.setDrawHighlightCircleEnabled(true);
        set1.setDrawHighlightIndicators(false);

        ArrayList<IRadarDataSet> sets = new ArrayList<>();
        sets.add(set1);

        RadarData data = new RadarData(sets);
        data.setValueTextSize(8f);
        data.setDrawValues(false);
        data.setValueTextColor(ContextCompat.getColor(context, R.color.black));

        mChart.setData(data);
        mChart.invalidate();
    }

    @SuppressLint("CheckResult")
    public void setNewData(HLPeopleDetailEntity pNewData) {
        ImageLoaderHelper.getInstance().load(context, pNewData.getAvatar(), mCivAvatar);
        mTvNickname.setText(pNewData.getNickname());
        mTvTag1.setText(pNewData.getTag_1());
        mTvTag2.setText(pNewData.getTag_2());
        mTextView16.setText(pNewData.getNickname());
        mTextView19.setText(pNewData.getPhone());
        mTextView22.setText(pNewData.getEmail());
        mTextView25.setText(pNewData.getDepartment());
        //mTextView28.setText(pNewData.getPosition());
        mTextView28.setText(pNewData.getTag_2());
        mTextView31.setText(pNewData.getIntroduce());
        mTextView34.setText(pNewData.getServiceContent());

        RxView.clicks(mTextView19).throttleFirst(1, TimeUnit.SECONDS).subscribe(v -> {
            rxPermissions.request(Manifest.permission.CALL_PHONE).subscribe(b -> {
                RxDeviceTool.callPhone(context, pNewData.getPhone());
            });
        });
        //聊天
        RxView.clicks(mIvSendMessage).throttleFirst(1, TimeUnit.SECONDS).subscribe(v -> {
            ARouter.getInstance().build(ARouterConstant.HLChatActivity)
                    .withString("id", id)
                    .withString("name", pNewData.getNickname())
                    .navigation();
        });
        if (pNewData.getHonor().size() <= 0) mLlHonor.setVisibility(View.GONE);
        HLPeopleHonorAdapter honorAdapter = new HLPeopleHonorAdapter(pNewData.getHonor());
        mRvList.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
        mRvList.setAdapter(honorAdapter);
        setChart(pNewData.getDate_arr());
    }

    @Override
    protected void onResume() {
        super.onResume();
        getP().getData(id);
    }
}
